<script setup>
import { ElLoading } from 'element-plus'
import { resetData } from '@/hooks/use-common'
import { uploadFile } from '@/api/system/upload'
import { i18n } from '@/lang'
const { t } = i18n.global

//element valid
const loading = ref(false)
/** 提交按钮 */
const open = ref(false)
const title = ref('')
const emits = defineEmits([])
const uploadType = ref(0)

/** 取消按钮 */
const cancel = () => {
  open.value = false
  // emits('getList')
}

const uploadRef = ref()
const onChange = () => {
  uploadRef.value?.submit()
}
const hanldeHttpRequest = (options) => {
  const formData = new FormData()
  formData.append('file', options.file)
  loading.value = true
  const loadingInstance = ElLoading.service({ fullscreen: true })
  uploadFile(formData).then(() => {
    loading.value = false
    loadingInstance.close()
  })
}

const showModal = ({ type }) => {
  title.value = t('ossIndex.uploadModel')
  uploadType.value = type
  open.value = true
}

onMounted(() => {})
//导出给refs使用
//用于子组件向其父组件传值
defineExpose({ cancel, showModal })
</script>

<template>
  <el-dialog
    v-model="open"
    :title="title"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    width="500px"
    append-to-body
    @close="cancel"
  >
    <el-upload
      ref="uploadRef"
      class="upload-demo"
      drag
      :limit="1"
      accept=".obj,.ply,.stl"
      :auto-upload="false"
      :show-file-list="false"
      :on-change="onChange"
      :http-request="hanldeHttpRequest"
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">
        Drop file here or
        <em>click to upload</em>
      </div>
      <div class="el-upload__tip">please select .stl,.obj,.ply</div>
    </el-upload>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="cancel">{{ t('ossIndex.confirm') }}</el-button>
        <!-- <el-button type="primary" @click="cancel">{{ t('ossIndex.cancel') }}</el-button> -->
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss"></style>
